<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

 



class Newk extends Home_Controller 
{
	/**
     * 当前uri
     *
     * @access private
     * @var string
     */
	 private $_uri = '';

	/**
     * 当前页码
     *
     * @access private
     * @var string
     */
	 private $_current_page = 1;
	
	/**
     * 每页条目数
     *
     * @access private
     * @var int
     */
	 private $_limit = 5;

	/**
     * 偏移
     *
     * @access private
     * @var int
     */
	 private $_offset = 0;

	/**
     * 条目总数
     *
     * @access private
     * @var int
     */
	 private $_total_count = 0;

	/**
     * 条目总数
     *
     * @access private
     * @var array
     */
	 private $_posts = array();

	/**
     * 分页字符串 wrapper
     *
     * @access private
     * @var string
     */
	 private $_pagination = '';
	 
	 /**
     * 构造函数
     * 
     * @access public
     * @return void
     */




 /**
     * 初始化分页参数
     * 
     * @access public
     * @param  int  $current_page
     * @return void
     */
	private function _init_pagination($current_page)
	{
		/** 当前页 */
		$this->_current_page = ($current_page && is_numeric($current_page)) ? intval($current_page) : 1;
		
		/** 每页多少项 */
		$page_size = setting_item('posts_page_size');
		$this->_limit = ($page_size && is_numeric($page_size)) ? intval($page_size) : 5;
		
		/** 偏移量 */
		$this->_offset = ($this->_current_page - 1) * $this->_limit;
		
		if($this->_offset < 0)
		{
			redirect(site_url());
		}
	}







	public function __construct()
	{
		parent::__construct();

		
	}

	public function index()
	{
		$this->load->helper('Func'); 
		$data=$this->_home();
		/** 加载主题下的页面 */
		$this->load_home_view('home', $data);
		//print_r($data);
	}









	//首页模块
	public function _home()
	{
		$this->load->library('Kclass'); //载入pChart库		
		$this->load->library('Head'); //载入头部库	
		$data = $this->head->def();		


		$data['new_list'] = $this->kclass->news('','','','15');//新闻
		$data['new_head_list'] = $this->kclass->news('nhead','1','','2');//新闻
		$data['new_ncommend_list'] = $this->kclass->news('ncommend','1','','12');//新闻
		$data['new_zhuanti_list'] = $this->kclass->zhuanti('','1','','10');//专题	
		$data['new_hdp_list'] = $this->kclass->hdp(6);//幻灯片	
		return $data;
	}






//模块处理/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//相关调用
	public function k_______u($t)
	{
	
	
	$this->load->library('Kclass'); //载入pChart库	
	$this->load->helper('Func'); 
	if($t<4){
		$data['typeid']=$t;
	
			$this->load_home_view('qita', $data);
	
		}
	}
	
	
	
//模块处理/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//评论管理
	public function k_______pl($kid='',$typeid='')
	{
	

	$this->load->library('Kclass'); //载入pChart库	
	$this->load->helper('Func'); 
	if(!empty($kid) && !empty($typeid)){
		
		$bs=$this->kclass->bbs_wz($kid,$typeid);
		echo $bs['content'];
	}	

	}	
	
//搜索模块/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//专题相关调用
	public function k_______s($keyword)
	{
		
		$data=$this->_ks(urldecode($keyword),1);

		if(!empty($data['result'])){
			$this->load_home_view('ks', $data);
		}
	}

	public function _ks($keyword,$i)
	{
		$keyword=str_replace('-',',',$keyword);
		$this->load->library('Kclass'); //载入pChart库	
		$this->load->helper('Func'); 

		if(!empty($i)){

			$data['result'] = $this->_mysql_js($keyword,'ktitle,kcontent,kid,kimage,listid,nshow','king__article','kid','article wzzl','5');
			$data['zt'] = $this->_mysql_js($keyword,'ktitle,kcontent,kid,kimage,listid,nshow','king__zhuanti','kid','zhuanti ztzl','5');
		}else{
			$data['result'] = $this->kclass->m_search($keyword,'5');//新闻
			$data['zt'] = $this->kclass->m_search($keyword,'5','king__zhuanti');//专题

		}
			
			$data['tag'] = $this->kclass->tag_search($keyword);//标签
			$data['keywords'] = $keyword;
		return $data;
	}	
	
//模块处理/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//长尾关键词
		
	
	public function newtag($kid='')
	{

	if(!$kid) show_404();
	
	$this->load->library('Head'); //载入头部库		
	$this->load->library('Kclass'); //载入pChart库	
	$this->load->helper('Func'); 
	$array['kid']=$kid;
	$bs=$this->kclass->new_row('ktitle,ktag','king_key',$array,"ndate desc");



	$data=$this->_ks($bs['ktitle'],'1'); //开启全文检索搜索标题 1为开启	



	$data['sj']=$this->kclass->sj_search($bs['ktag'],'6'); #---------------------------------------------------搜索
	$data['bbs']=$this->kclass->bbs_search($bs['ktag'],'6'); #---------------------------------------------------搜索
	$data['key']=$this->kclass->key_search($bs['ktag'],'6'); #---------------------------------------------------搜索
	$data['newkey']=$this->kclass->key_search('','6'); #---------------------------------------------------搜索

	
	
	$data['title']=$bs['ktitle'];
	if(empty($data['title'])) show_404();
	$this->load_home_view('keywords', $data);
	}	
	
	

	public function _mysql_js($keyword,$select,$from,$wherin,$name,$num){
			$wz=$this->_qwjs($keyword,$name,$num);
			if($wz['total']!=0){
			$this->db->select($select)->from($from);
			$this->db->where('nshow = 1');	
			$this->db->where_in($wherin,$wz['kid']);	
			$this->db->limit($num);// 按分页取出数据
			$res=$this->db->get()->result_array();	
			}
			return $res;
	}

	
	public function _qwjs($title,$name='*',$num='100')
	{

		$this->load->library('Sphinxclient');
     	$mode = SPH_MATCH_ANY;
		$host = "127.0.0.1";
		$port = 9312;
		$index = $name;
		$groupby = "";
				$groupsort = "@kid desc";
				$filter = "kid";
		$filtervals = array();
		$distinct = "";
		$sortby = "";
		$limit = $num;
		$ranker = SPH_RANK_PROXIMITY_BM25;
		$select = "";
		
		$this->sphinxclient->SetServer ( $host, $port );
		$this->sphinxclient->SetConnectTimeout ( 1 );
		$this->sphinxclient->SetWeights ( array ( 100, 1 ) );
		$this->sphinxclient->SetMatchMode ( $mode );
		if ( count($filtervals) )    $this->sphinxclient->SetFilter ( $filter, $filtervals );
		if ( $groupby )               $this->sphinxclient->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
		$this->sphinxclient->SetSortMode ( SPH_SORT_RELEVANCE, $sortby );
		
		if ( $distinct )            $this->sphinxclient->SetGroupDistinct ( $distinct );
		if ( $select )                $this->sphinxclient->SetSelect ( $select );
		if ( $limit )                $this->sphinxclient->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
		$this->sphinxclient->SetRankingMode ( $ranker );
		$this->sphinxclient->SetArrayResult ( true );
		$res = $this->sphinxclient->Query ($title, $index);

			if(!empty($res)){
				foreach($res['matches'] as $key => $value){
					$kid[$key]=$value['id'];
					$total=$key;
				}

			}
				$data['kid']=$kid;
				$data['total']=$total;			
				
			return $data;
		
	}
	
	

	
	
	
	
	
	public function foont($a)
	{
		if(empty($a))return '';
		$this->load->library('Head'); //载入头部库		
		$this->load->library('Kclass'); //载入pChart库	
		echo $this->kclass->ad_row($a);
	}	
		
	
	

}
